module Trails
  module Twilio
    class Incoming
      attr_reader :account
      def initialize( request, opts = {} )
        @request = request
        @account = Trails::Twilio::Account.from_request( request )
      end

      def twilio_data
        @twilio_data ||= request.params.slice( INCOMING_VARS ).dup
      end

      def is_sms?
        !sms_message_sid.blank?
      end

      protected
      attr_reader :request

      INCOMING_VARS = [
                       # Always available:
                       'CallGuid',       # A unique identifier for this call, generated by Twilio. It's 34 characters long, and always starts with the letters CA.
                       'Caller',         # The phone number of the party that initiated the call. If the call is inbound, then it is the caller's caller-id. If the call is outbound, i.e., initiated by making a request to the REST Call API, then this is the phone number you specify as the caller-id.
                       'Called',         # The phone number of the party that was called. If the call is inbound, then it's your application phone number. If the call is outbound, then it's the phone number you provided to call.
                       'AccountGuid',    # Your Twilio account number which is the Twilio Account GUID for the call. It is 34 characters long, and always starts with the letters AC.
                       'CallStatus',     # The status of the phone call. The value can be "in-progress", "completed", "busy", "failed" or "no-answer". For a call that was answered and is currently going on, the status would be "in-progress". For a call that couldn't be started because the called party was busy, didn't pick up, or the number dialed wasn't valid: "busy", "no-answer", or "failed" would be returned. If the call finished because the call ended or was hung up, the status would be "completed".
                       'CallerCity',	# The city of the caller.
                       'CallerState',	# The state or province of the caller.
                       'CallerZip',	# The postal code of the caller.
                       'CallerCountry',	# The country of the caller.
                       'CalledCity',	# The city of the called party.
                       'CalledState',	# The state or province of the called party.
                       'CalledZip',	# The postal code of the called party.
                       'CalledCountry',	# The country of the called party.
                       # Gather:
                       'Digits',         # The digits received from the caller

                       'RecordingUrl',   # The URL of the recorded audio file
                       'Duration', 	# The time duration of the recorded audio file
                       'Digits',         # What (if any) key was pressed to end the recording

                       # SMS:
                       'SmsMessageSid',  # Message SID
                       'AccountSid',     # Account ID
                       'From',           # 
                       'To',             #
                       'Body',           # 160 chars

                      ]
      public
      INCOMING_VARS.uniq.each do |pname|
        mname = pname.gsub( /[A-Z]/ ) { |s| "_#{s.downcase}" }.gsub( /^_/, '' )
        # some extra debugging code here:
        # ActionController::Base.logger.debug{ "defining method: #{mname} for param #{pname}" }
        define_method( mname ) do
          return request.params[ pname ] || request.env["HTTP_X_TWILIO_#{pname.upcase}"]
        end # define_method
      end # each 

    end #
  end # module Twilio
end # module Trails
